package cn.xiyue.production.ui;

import android.os.Bundle;
import android.widget.TextView;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.xiyue.production.R;
import cn.xiyue.production.base.CallableActivity;
import cn.xiyue.production.bean.Car;
import cn.xiyue.production.bean.Store;
import cn.xiyue.production.network.ResponseData;
import retrofit2.Response;

public class FinishedCarActivity extends CallableActivity {

    private SmartTableView stb_finished;
    private TextView tv_finished_info;
    private List<Store> data;
    private Map<Integer, Car> carMap;
    private int allSize = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_finished_car);
        initView();
    }

    private void initView() {
        stb_finished = findViewById(R.id.stb_finished);
        tv_finished_info = findViewById(R.id.tv_finished_info);
        async(requestApi.getAllStore(), value -> {
            data = value.getData();
            carMap = new HashMap<>();
            getInfo();
        });
    }

    private void getInfo() {
        new Thread(() -> {
            for (Store store : data) {
                try {
                    if (carMap.get(store.getCarTypeId()) == null) {
                        Response<ResponseData<Car>> execute = requestApi.getCar(store.getCarTypeId()).execute();
                        List<Car> data = execute.body().getData();
                        carMap.put(store.getCarTypeId(), data.get(0));
                    }
                    String typeName = store.getType() == 0 ? "正常" : "维修";
                    store.setTypeName(typeName);
                    Car car = carMap.get(store.getCarTypeId());
                    store.setCarTypeName(car.getCarTypeName());
                    store.setSize(car.getSize());
                    allSize = allSize + store.getNum() * store.getSize();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            stb_finished.post(() -> {
                stb_finished.setData(data, Store.class);
                tv_finished_info.setText(String.format("成品车辆库存信息：共占地%d，库存容量30000", allSize));
            });
        }).start();
    }
}